package com.imut.lagain.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imut.lagain.entity.MenstrualCycle;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

/**
 * 生理期记录数据访问层
 */
@Mapper
@Repository
public interface MenstrualCycleRepository extends BaseMapper<MenstrualCycle> {
    
    /**
     * 根据用户ID查询生理期记录
     * @param userId 用户ID
     * @param page 分页参数
     * @return 生理期记录分页列表
     */
    @Select("SELECT * FROM menstrual_cycle WHERE user_id = #{userId} AND is_deleted = 0 ORDER BY start_date DESC")
    IPage<MenstrualCycle> findByUserId(@Param("userId") Long userId, Page<MenstrualCycle> page);
    
    /**
     * 根据用户ID和日期范围查询生理期记录
     * @param userId 用户ID
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 生理期记录列表
     */
    @Select("SELECT * FROM menstrual_cycle WHERE user_id = #{userId} AND start_date >= #{startDate} AND start_date <= #{endDate} AND is_deleted = 0 ORDER BY start_date ASC")
    List<MenstrualCycle> findByUserIdAndDateRange(@Param("userId") Long userId, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);
    
    /**
     * 获取用户最近的生理期记录
     * @param userId 用户ID
     * @param limit 限制数量
     * @return 生理期记录列表
     */
    @Select("SELECT * FROM menstrual_cycle WHERE user_id = #{userId} AND is_deleted = 0 ORDER BY start_date DESC LIMIT #{limit}")
    List<MenstrualCycle> findRecentByUserId(@Param("userId") Long userId, @Param("limit") int limit);
    
    /**
     * 获取用户最后一次生理期记录
     * @param userId 用户ID
     * @return 生理期记录
     */
    @Select("SELECT * FROM menstrual_cycle WHERE user_id = #{userId} AND is_predicted = 0 AND is_deleted = 0 ORDER BY start_date DESC LIMIT 1")
    MenstrualCycle findLastActualCycleByUserId(@Param("userId") Long userId);
    
    /**
     * 删除用户的预测数据
     * @param userId 用户ID
     * @return 影响行数
     */
    @Delete("DELETE FROM menstrual_cycle WHERE user_id = #{userId} AND is_predicted = 1")
    int deletePredictedDataByUserId(@Param("userId") Long userId);
    
    /**
     * 查询指定日期是否有生理期记录
     * @param userId 用户ID
     * @param date 日期
     * @return 生理期记录
     */
    @Select("SELECT * FROM menstrual_cycle WHERE user_id = #{userId} AND start_date <= #{date} AND (end_date >= #{date} OR end_date IS NULL) AND is_deleted = 0 LIMIT 1")
    MenstrualCycle findByUserIdAndDate(@Param("userId") Long userId, @Param("date") LocalDate date);
    
    /**
     * 统计用户的平均周期长度
     * @param userId 用户ID
     * @param limit 统计最近几次记录
     * @return 平均周期长度
     */
    @Select("SELECT AVG(cycle_length) FROM menstrual_cycle WHERE user_id = #{userId} AND cycle_length IS NOT NULL AND is_predicted = 0 AND is_deleted = 0 ORDER BY start_date DESC LIMIT #{limit}")
    Double getAverageCycleLength(@Param("userId") Long userId, @Param("limit") int limit);
    
    /**
     * 统计用户的平均经期长度
     * @param userId 用户ID
     * @param limit 统计最近几次记录
     * @return 平均经期长度
     */
    @Select("SELECT AVG(period_length) FROM menstrual_cycle WHERE user_id = #{userId} AND period_length IS NOT NULL AND is_predicted = 0 AND is_deleted = 0 ORDER BY start_date DESC LIMIT #{limit}")
    Double getAveragePeriodLength(@Param("userId") Long userId, @Param("limit") int limit);
}